
package webdev.danagtravel;

import java.io.IOException;

import webdev.common.Common;
import webdev.database.DatabaseHelp;
import webdev.imgloader.AnimationFactory;
import webdev.imgloader.ImageLoader;
import webdev.imgloader.ImageLoaderCircle;
import webdev.imgloader.ImageLoaderLocal;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.ImageView;
import dev.danang.R;

public class WellcomeActivity extends Activity implements OnClickListener {

	private Animation animation;
	private ImageView background;
	boolean _position_right = true;
	private int position = 1;
	
	private Button mSignIn;
	private Button mSignUp;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// set screen portrait
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(R.layout.activity_sign_in);
		DatabaseHelp database = new DatabaseHelp(this);
		try {
			database.createDataBase();
		} catch (IOException e) {
			e.printStackTrace();
		}
		init();
		left();		
	}
	private void init() {
		background = (ImageView) findViewById(R.id.background_sign_in);
		mSignIn = (Button) findViewById(R.id.btn_sign_in);
		mSignIn.setOnClickListener(this);
		mSignUp = (Button) findViewById(R.id.btn_sign_up);
		mSignUp.setOnClickListener(this);

		/* Init image loader for application */
        Common.IMAGE_LOADER_YOULOOK = new ImageLoader(getApplicationContext());
        Common.IMAGE_LOADER_CIRCLE_YOULOOK = new ImageLoaderCircle(getApplicationContext());
        Common.IMAGE_LOADER_LOCAL_YOULOOK = new ImageLoaderLocal(getApplicationContext());
	}
	
	public void left() {
		animation = AnimationFactory.toLeft1();
		background.setAnimation(animation);
		animation.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation arg0) {
			}
			
			@Override
			public void onAnimationRepeat(Animation arg0) {
				
			}
			
			@Override
			public void onAnimationEnd(Animation arg0) {
				background.setVisibility(View.GONE);
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				_position_right = false;
				right();
			}
		});
	}
	
	public void hide() {
		animation = AnimationFactory.alphaHide();
		background.setAnimation(animation);
		animation.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation arg0) {
			}
			
			@Override
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animation arg0) {
				setImage();
				show();
			}
		});
	}
	
	public void show() {
		animation = AnimationFactory.alphaShow();
		background.setAnimation(animation);
		animation.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation arg0) {
			}
			
			@Override
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animation arg0) {
				if (_position_right) {
					left();
				} else {
					right();
				}
			}
		});
	}
	
	public void right() {
		animation = AnimationFactory.toRight1();
		background.setAnimation(animation);
		animation.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation arg0) {
			}
			
			@Override
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animation arg0) {
				_position_right = true;
				hide();
			}
		});
	}
	
	public void setImage() {
		if (position > 2) {
			position = 0;
		}
		switch (position) {
		case 0:
			background.setImageResource(R.drawable.img_sign_in1);
			break;
		case 1:
			background.setImageResource(R.drawable.img_sign_in2);
			break;
		case 2:
			background.setImageResource(R.drawable.img_sign_in3);
			break;
		default:
			break;
		}
		position++;
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
//		facebook.authorizeCallback(requestCode, resultCode, data);
	}
	/**
	*
	* 
	* @author: truong.le
	* @datetime: Nov 12, 2013 11:12:19 AM
	* @return: voidTODO
	* 
	*/
	public void loginSuccess()
	{
		Intent intent = new Intent(WellcomeActivity.this,LoginActivity.class);
		startActivity(intent);
	}
	/*public void login(Boolean isError,String mId,String mEmail, String mPassword, String mDisplayName)
	{
		if(isError == true){			
			Common.USER_INFOMATION = new UserLogin();
			Common.USER_INFOMATION.setId(mId);
			Common.USER_INFOMATION.setPassword(mPassword);
			Common.USER_INFOMATION.setDisplayname(mDisplayName);
			Common.USER_INFOMATION.setEmail(mEmail);
			Common.USER_INFOMATION.setUrlAvatar(mUrlAvatar);
			// --> Change value checkLogined --> true;
			Common.checkLogined = true;
		}
		Intent intent = new Intent(SignInActivity.this,CategoryCheckActivity.class);
		startActivity(intent);
	}*/
	
	
	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.btn_sign_in:
//			finish();
			intent = new Intent(WellcomeActivity.this, LoginActivity.class);
			startActivity(intent);
			finish();
			break;
		case R.id.btn_sign_up:
			intent = new Intent(WellcomeActivity.this, SignUpActivity.class);
			startActivity(intent);
			finish();
//			SQLQuery sql = new SQLQuery(WellcomeActivity.this);
//			sql.setAllDiaDiem();
			break;
		default:
			break;
		}
	}
	@SuppressWarnings("deprecation")
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		 /*if (Integer.parseInt(android.os.Build.VERSION.SDK) < 5
                 && keyCode == KeyEvent.KEYCODE_BACK
                 && event.getRepeatCount() == 0) {*/
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
             Log.d("CDA", "onKeyDown Called");
             onBackPressed();
         }
		return super.onKeyDown(keyCode, event);
	}
	public void onBackPressed() {
        Log.d("CDA", "onBackPressed Called");
        finish();
        
        return;
    }
}
